package com.collect.tanzania.zhiScene.zhiWidget.zhiPopupWindow;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.collect.tanzania.R;
import com.collect.tanzania.zhiScene.zhiTelemarketing.zhiLogic.ZHITelemarketingFollowHelper;
import com.collect.tanzania.zhiScene.zhiWidget.zhiStepView.ZHINextStepView;
import com.collect.tanzania.zhiUtils.DisplayUtil;

public class ZHIPopupTelemaketingFollow implements PopupWindow.OnDismissListener {

    private final Context mContext;
    private final View mAnchorView;
    private View mContentAtAboveView;
    private View mContentAtBelowView;
    private PopupWindow mPopupWindow;

    private ZHIPopupTelemaketingFollowItem down_ptfi_unfollowed;
    private ZHIPopupTelemaketingFollowItem down_ptfi_followed;
    private ZHIPopupTelemaketingFollowItem down_ptfi_focus_on;
    private ZHINextStepView down_nsv_ok;

    private ZHIPopupTelemaketingFollowItem up_ptfi_unfollowed;
    private ZHIPopupTelemaketingFollowItem up_ptfi_followed;
    private ZHIPopupTelemaketingFollowItem up_ptfi_focus_on;
    private ZHINextStepView up_nsv_ok;

    private boolean mShowAtBelow = true;
    private int mContentViewWidth;
    private OnZHIPopupTelemaketingFollowListener mListener;

    public static void showPopWindow(Context context, View anchorView, OnZHIPopupTelemaketingFollowListener listener) {
        ZHIPopupTelemaketingFollow popupTelemaketingFollow = new ZHIPopupTelemaketingFollow(context, anchorView, listener);
        popupTelemaketingFollow.show();
    }

    public ZHIPopupTelemaketingFollow(Context context, View anchorView, OnZHIPopupTelemaketingFollowListener listener) {
        mContext = context;
        mAnchorView = anchorView;
        mListener = listener;
        initViews();
    }

    @Override
    public void onDismiss() {

    }

    @SuppressLint("InflateParams")
    private void initViews() {
        try {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mContentAtBelowView = inflater.inflate(R.layout.pop_telemaketing_follow_down, null);
            mContentAtAboveView = inflater.inflate(R.layout.pop_telemaketing_follow_up, null);

            down_ptfi_unfollowed = mContentAtBelowView.findViewById(R.id.down_ptfi_unfollowed);
            down_ptfi_followed = mContentAtBelowView.findViewById(R.id.down_ptfi_followed);
            down_ptfi_focus_on = mContentAtBelowView.findViewById(R.id.down_ptfi_focus_on);
            down_nsv_ok = mContentAtBelowView.findViewById(R.id.down_nsv_ok);

            down_ptfi_unfollowed.setCheck(false);
            down_ptfi_unfollowed.setContent("Unfollowed");
            down_ptfi_followed.setCheck(false);
            down_ptfi_followed.setContent("Followed");
            down_ptfi_focus_on.setCheck(false);
            down_ptfi_focus_on.setContent("Focus on");

            up_ptfi_unfollowed = mContentAtAboveView.findViewById(R.id.up_ptfi_unfollowed);
            up_ptfi_followed = mContentAtAboveView.findViewById(R.id.up_ptfi_followed);
            up_ptfi_focus_on = mContentAtAboveView.findViewById(R.id.up_ptfi_focus_on);
            up_nsv_ok = mContentAtAboveView.findViewById(R.id.up_nsv_ok);

            up_ptfi_unfollowed.setCheck(false);
            up_ptfi_unfollowed.setContent("Unfollowed");
            up_ptfi_followed.setCheck(false);
            up_ptfi_followed.setContent("Followed");
            up_ptfi_focus_on.setCheck(false);
            up_ptfi_focus_on.setContent("Focus on");

            down_ptfi_unfollowed.setOnClickListener(v -> {
                down_ptfi_unfollowed.setCheck(true);
                down_ptfi_followed.setCheck(false);
                down_ptfi_focus_on.setCheck(false);
            });

            down_ptfi_followed.setOnClickListener(v -> {
                down_ptfi_unfollowed.setCheck(false);
                down_ptfi_followed.setCheck(true);
                down_ptfi_focus_on.setCheck(false);
            });

            down_ptfi_focus_on.setOnClickListener(v -> {
                down_ptfi_unfollowed.setCheck(false);
                down_ptfi_followed.setCheck(false);
                down_ptfi_focus_on.setCheck(true);
            });

            down_nsv_ok.setOnMenuItemDidChangedListener(item -> {
                if (mListener != null) {
                    String content = "";
                    if (down_ptfi_followed.isChecked()) {
                        content = ZHITelemarketingFollowHelper.FOLLOW_STATE_FOLLOWED;
                    } else if (down_ptfi_focus_on.isChecked()) {
                        content = ZHITelemarketingFollowHelper.FOLLOW_STATE_FOCUS_ON;
                    }
                    mListener.onConfirm(content);

                    if (mPopupWindow != null) {
                        mPopupWindow.dismiss();
                    }
                }
            });

            up_ptfi_unfollowed.setOnClickListener(v -> {
                up_ptfi_unfollowed.setCheck(true);
                up_ptfi_followed.setCheck(false);
                up_ptfi_focus_on.setCheck(false);
            });

            up_ptfi_followed.setOnClickListener(v -> {
                up_ptfi_unfollowed.setCheck(false);
                up_ptfi_followed.setCheck(true);
                up_ptfi_focus_on.setCheck(false);
            });

            up_ptfi_focus_on.setOnClickListener(v -> {
                up_ptfi_unfollowed.setCheck(false);
                up_ptfi_followed.setCheck(false);
                up_ptfi_focus_on.setCheck(true);
            });

            up_nsv_ok.setOnMenuItemDidChangedListener(item -> {
                if (mListener != null) {
                    String content = "";
                    if (up_ptfi_followed.isChecked()) {
                        content = ZHITelemarketingFollowHelper.FOLLOW_STATE_FOLLOWED;
                    } else if (up_ptfi_focus_on.isChecked()) {
                        content = ZHITelemarketingFollowHelper.FOLLOW_STATE_FOCUS_ON;
                    }
                    mListener.onConfirm(content);

                    if (mPopupWindow != null) {
                        mPopupWindow.dismiss();
                    }
                }
            });
//            RelativeLayout fl_content_bg = mContentAtBelowView.findViewById(R.id.rl_follow_content_bg);
//            fl_content_bg.setOnClickListener(v -> {
//                if (mPopupWindow != null) {
//                    mPopupWindow.dismiss();
//                }
//            });

            //计算宽高
//            int w = View.MeasureSpec.makeMeasureSpec(0,
//                    View.MeasureSpec.UNSPECIFIED);
//            int h = View.MeasureSpec.makeMeasureSpec(0,
//                    View.MeasureSpec.UNSPECIFIED);
//            fl_content_bg.measure(w, h);
//            int fbgWidth = fl_content_bg.getMeasuredWidth();

            // 获取锚点View在屏幕上的左上角坐标位置
            final int[] anchorLoc = new int[2];
            mAnchorView.getLocationOnScreen(anchorLoc);
            mContentViewWidth = mAnchorView.getWidth();

            int maxItemWidth = down_ptfi_unfollowed.getMinDisplayWidth();
            mContentViewWidth = Math.max(mContentViewWidth, maxItemWidth);

            mPopupWindow = new PopupWindow(mAnchorView, mContentViewWidth, RelativeLayout.LayoutParams.WRAP_CONTENT);
            mPopupWindow.setFocusable(true);
            mPopupWindow.setOutsideTouchable(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void show() {
        if (!mPopupWindow.isShowing()) {
            showPopup();
        } else {
            mPopupWindow.setOnDismissListener(this);
            mPopupWindow.dismiss();
        }
    }

    private void showPopup() {
        int[] windowPos = calculatePopWindowPos(mAnchorView, mContentAtBelowView);
        mPopupWindow.setContentView(mShowAtBelow ? mContentAtBelowView : mContentAtAboveView);
        mPopupWindow.showAtLocation(mAnchorView, Gravity.TOP | Gravity.START, windowPos[0], windowPos[1]);
    }

    /**
     * 计算出来的位置，y方向就在anchorView的上面和下面对齐显示，x方向就是与屏幕右边对齐显示
     * 如果anchorView的位置有变化，就可以适当自己额外加入偏移来修正
     *
     * @param anchorView  呼出window的view
     * @param contentView window的内容布局
     * @return window显示的左上角的xOff, yOff坐标
     */
    private int[] calculatePopWindowPos(final View anchorView, final View contentView) {
        final int[] windowPos = new int[2];
        try {
            final int[] anchorLoc = new int[2];
            // 获取锚点View在屏幕上的左上角坐标位置
            anchorView.getLocationOnScreen(anchorLoc);
            final int anchorHeight = anchorView.getHeight();
            // 获取屏幕的高宽
            final int screenHeight = DisplayUtil.getHeight(anchorView.getContext());
            final int screenWidth = DisplayUtil.getWidth(anchorView.getContext());
            contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            // 计算contentView的高宽
            final int windowHeight = contentView.getMeasuredHeight();

            final int windowWidth = contentView.getMeasuredWidth();
            // 判断需要向上弹出还是向下弹出显示
            final boolean isNeedShowUp = (screenHeight - anchorLoc[1] - anchorHeight < windowHeight);
            if (isNeedShowUp) {
                mShowAtBelow = false;
                windowPos[0] = anchorLoc[0];//screenWidth - windowWidth;
                windowPos[1] = anchorLoc[1] - windowHeight; //ZHIStatusBar.getStatusBarHeight(mContext);
            } else {
                mShowAtBelow = true;
                windowPos[0] = anchorLoc[0];//screenWidth - windowWidth;
                windowPos[1] = anchorLoc[1] + anchorHeight;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return windowPos;
    }

    public interface OnZHIPopupTelemaketingFollowListener {
        void onConfirm(String followState);
    }
}
